package com.hb.software.controller.attendance;

import com.hb.software.attendance.AttendanceService;

import com.hb.software.controller.BaseController;
import com.hb.software.domain.Result;

import com.hb.software.entity.attendance.Attendance;
import com.hb.software.entity.attendance.AttendanceMessage;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("attendance")
public class AttendanceController extends BaseController<AttendanceMessage,Long,AttendanceService> {

    private static final Logger logger = LogManager.getLogger(AttendanceController.class.getName());


    private AttendanceService attendanceService;

    @Autowired
    public void setAttendanceService(AttendanceService attendanceService) {
        this.attendanceService = attendanceService;
        this.manager = this.attendanceService;
    }

    /**
     * 通过id查询员工信息
     */
    @PostMapping("/findLastId")
    public Result findLastId()   {
        long id = attendanceService.findLastId();
        return Result.success(id);
    }
    @PostMapping("/exportExcel")
    public Result exportExcel(@RequestBody String path)throws UnsupportedEncodingException {
        //处理中文乱码问题
        path = URLDecoder.decode(path,"UTF-8");
        path = path.substring(0,path.length()-1);
        List list = attendanceService.findAll();

        String str = attendanceService.createExcel(list,path);
        if(str.equals("success"))
            return Result.success();
        else return Result.error("路径错误");
    }
    @PostMapping("/findByName")
    public Result findByName(@RequestBody String name) throws UnsupportedEncodingException {
        //处理中文乱码问题
        name = URLDecoder.decode(name,"UTF-8");
        name = name.substring(0,name.length()-1);
        logger.info(name);
        List list=attendanceService.findByName(name);
        return Result.success(list);
    }
    @PostMapping("/findByData")
    public Result findByData(@RequestBody String data) {
        data = data.substring(0,data.length()-1);
        logger.info(data);
        List list=attendanceService.findByData(data);
        return Result.success(list);
    }
    @PostMapping("/findByTime")
    public Result findByTime(@RequestBody String time) {
        time = time.substring(0,time.length()-1);
        logger.info(time);
        List list=attendanceService.findByData(time);
        return Result.success(list);
    }
    @PostMapping("/getAttendance")
    public Result save(@RequestBody List<String> list ){
        AttendanceMessage attendance = new AttendanceMessage();
        try {
            attendance.setId(attendanceService.findLastId()+1);
        }catch (Exception e) {
            attendance.setId(1L);
        }
        attendance.setName(list.get(0));
        attendance.setSignInDate(list.get(1));
        attendance.setSignInTime(list.get(2));

        manager.save(attendance);
        return Result.success();

    }



}
